package com.diorsunion.hedge.client.borker.ib;

import com.diorsunion.hedge.borker.BorkerClient;
import com.diorsunion.hedge.common.ConnectionStatus;
import com.diorsunion.hedge.common.ServiceResult;
import com.diorsunion.hedge.util.LogUtil;
import com.ib.client.EClientSocket;
import com.ib.controller.AccountSummaryTag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by harley-dog on 2016/5/6.
 */
@Component
@PropertySource("classpath:borker.properties")
public class IBClient implements BorkerClient {

    @Value("${borker.client.id}")
    int clientId;
    AtomicInteger reqIdCounter = new AtomicInteger();
    @Resource
    IBWrapper ibWrapper;
    EClientSocket twsClient;

    public IBClient(){
    }

    @Override
    public String getBorkerName() {
        return "盈透TWS";
    }

    @Override
    public ServiceResult<ConnectionStatus> connect(String address, int port) {
        try{
            twsClient.eConnect(address, port, clientId);
            if(twsClient.isConnected()){
                return ServiceResult.successResult(ConnectionStatus.OK);
            }
        }catch (Throwable e){
            LogUtil.error("连接IB客户端失败!",e);
            return ServiceResult.successResult(ConnectionStatus.NoOK,"连接盈透失败:"+e.getMessage());
        }
        return ServiceResult.successResult(ConnectionStatus.NoOK,"未知异常");

    }

    @Override
    public ServiceResult<ConnectionStatus> disConnect() {
        twsClient.eDisconnect();
        boolean b = twsClient.isConnected();
        return b?ServiceResult.successResult(ConnectionStatus.OK):ServiceResult.errorResult(ConnectionStatus.NoOK);

    }

    @Override
    public ServiceResult<ConnectionStatus> status() {
        boolean b = twsClient.isConnected();
        return b?ServiceResult.successResult(ConnectionStatus.OK):ServiceResult.errorResult(ConnectionStatus.NoOK);
    }

    @Override
    public ServiceResult<Boolean> beginDataTrans() {
        reqAccountSummary();//获取账户资产明细
        return ServiceResult.successResult(true);
    }

    @PostConstruct
    public void init(){
        twsClient = new EClientSocket(ibWrapper);
    }

    /**
     * 获取账户资产
     * @return
     */
    public ServiceResult reqAccountSummary() {
        int reqId = reqIdCounter.addAndGet(1);
        StringJoiner joiner = new StringJoiner(",");
        for(AccountSummaryTag tag: AccountSummaryTag.values()){
            joiner.add(tag.name());
        }
        twsClient.reqAccountSummary(reqId,"All", joiner.toString());
        return ServiceResult.successResult();
    }

    /**
     * 获取股票实时交易数据
     * @return
     */
    public ServiceResult reqAccountSummary1() {
        int reqId = reqIdCounter.addAndGet(1);
        StringJoiner joiner = new StringJoiner(",");
        for(AccountSummaryTag tag: AccountSummaryTag.values()){
            joiner.add(tag.name());
        }
        twsClient.reqAccountSummary(reqId,"All", joiner.toString());
        return ServiceResult.successResult();
    }

}
